﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApi.Bill.Biz;
using WebApi.Models;

namespace WebApi.Controllers
{
    public class TopicController : BaseController
    {

        #region 根据编码获取单条数据
        /// <summary>
        /// 根据编码获取单条数据
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<TopicModel> Get(int code)
        {
            ResponseData res = new ResponseData();
            TopicModel data = BizBase.Get<BizTopic>().Get(code);
            try
            {
                if (data != null)
                {
                    res.ErrorCode = 0;
                    res.Message = "Success";
                    res.DataList = new ModelData
                    {
                        TotalPage = 0,
                        DataList = data
                    };
                }
                else
                {
                    res.ErrorCode = 1;
                    res.Message = "暂无数据";
                    res.DataList = null;
                }

                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                return this.ControllerExHandler(res, "Get", ex);
            }
        }
        #endregion


        #region 获取当前议题信息列表
        /// <summary>
        /// GetPageToList
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<ResponseData> GetCurrentTopicPageToList([FromBody] dynamic param)
        {
            ResponseData res = new ResponseData();
            try
            {
                int recordCount = 0;
                int totalPage = 0;
                List<dynamic> resList = new List<dynamic>();
                List<TopicModel> data = BizBase.Get<BizTopic>().GetPageList(param, ref recordCount, ref totalPage);
                if (data != null && data.Count > 0)
                {
                    foreach (var p in data)
                    {
                        StringBuilder sb = new StringBuilder();
                        if (!string.IsNullOrEmpty(p.SJoinMans))
                        {
                            foreach (var item in p.SJoinMans.Split('|'))
                            {
                                sb.Append(item).Append(" ");
                            }
                        }
                        resList.Add(new { STopicName = p.STopicName, SJoinMans = sb.ToString(), STopicState = p.STopicState });
                    }
                    res.ErrorCode = 0;
                    res.Message = "success";
                    res.DataList = new ModelData
                    {

                        DataList = resList,
                        TotalPage = totalPage,
                        RecordCount = recordCount
                    };
                }
                return new JsonResult(res);

            }
            catch (Exception ex)
            {
                return this.ControllerExHandler(res, "GetCurrentTopicPageToList", ex);
            }

        }
        #endregion



    }
}
